package cn.gson.crmdemo.model.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crmdemo.model.dao.CpDao;
import cn.gson.crmdemo.model.dao.CustomerDao;
import cn.gson.crmdemo.model.dao.QuotationDao;
import cn.gson.crmdemo.model.dao.QuotationDetailsDao;
import cn.gson.crmdemo.model.dao.SalesOpportunityDao;
import cn.gson.crmdemo.model.mapper.QuotationMapper;
import cn.gson.crmdemo.model.pojo.Customer;
import cn.gson.crmdemo.model.pojo.Product;
import cn.gson.crmdemo.model.pojo.Quotation;
import cn.gson.crmdemo.model.pojo.Quotationdetail;
import cn.gson.crmdemo.model.pojo.Salesopportunity;
import cn.gson.crmdemo.model.vo.QuoDetailsVo;
import cn.gson.crmdemo.model.vo.QuoSaleCusProVo;
import cn.gson.crmdemo.model.vo.SaleVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class QuotationService {
	@Autowired
	QuotationDao qdao;
	@Autowired
	QuotationDetailsDao qddao;
	@Autowired
	QuotationMapper qm;
	@Autowired
	CustomerDao cdao;
	@Autowired
	SalesOpportunityDao sodao;
	@Autowired
	CpDao pdao;
	
	/**
	 * 查所有
	 * @return
	 */
	public JSONArray selectQuotation(){
		return qm.selectQuotation();
	}

	/*
	*//**
	 * 新增
	 * @param dept 由用户提交到Controller
	 */
	public void addQuo(QuoSaleCusProVo quotation) {
		Quotation quo = new Quotation();
		quo.setQuotationTheme(quotation.getQuotationTheme());
		quo.setQuotaionRemake(quotation.getQuotaionRemake());
		quo.setQuotationDate(quotation.getQuotationDate());
		quo.setQuotationReceiver(quotation.getQuotationReceiver());
		quo.setApprovalStatus(quotation.getApprovalStatus());
		quo.setQuotaionEmpName(quotation.getQuotaionEmpName());
		quo.setQuotationMoney(quotation.getQuotationMoney());
		quo.setPackExplain(quotation.getPackExplain());
		quo.setPayExplain(quotation.getPayExplain());
		
		Customer cusId = cdao.findById(quotation.getCustomer()).get();
		Salesopportunity saleId = sodao.findById(quotation.getSalesopportunity()).get();
		//System.err.println("ser==========="+quotation.getQuotationTheme()+quotation.getQuotationId());
		quo.setCustomer(cusId);
		quo.setSalesopportunity(saleId);
		qdao.save(quo);
	}
	/**
	 * 修改
	 * @param quotation
	 */
	public void updateQuos(QuoSaleCusProVo quotation) {
		Quotation quo = new Quotation();
		quo.setQuotationId(quotation.getQuotationId());
		quo.setQuotationTheme(quotation.getQuotationTheme());
		quo.setQuotaionRemake(quotation.getQuotaionRemake());
		quo.setQuotationDate(quotation.getQuotationDate());
		quo.setQuotationReceiver(quotation.getQuotationReceiver());
		quo.setApprovalStatus(quotation.getApprovalStatus());
		quo.setQuotaionEmpName(quotation.getQuotaionEmpName());
		quo.setQuotationMoney(quotation.getQuotationMoney());
		quo.setPackExplain(quotation.getPackExplain());
		quo.setPayExplain(quotation.getPayExplain());
		
		
		Customer cusId = cdao.findById(quotation.getCustomer()).get();
		Salesopportunity saleId = sodao.findById(quotation.getSalesopportunity()).get();
		//System.err.println("ser==========="+quotation.getQuotationTheme()+quotation.getQuotationId());
		quo.setCustomer(cusId);
		quo.setSalesopportunity(saleId);
		qdao.save(quo);
	}
	/**
	 * 根据id查
	 * @param quotationId
	 * @return
	 */
	public JSONArray selectquotationId(Integer quotationId) {
		return qm.selectQuotationId(quotationId);
	}
	/**
	 * 模糊查询名字
	 * @param quotationTheme
	 * @return
	 */
	public JSONArray selectMoHuQuotationTheme(String quotationTheme){
		return qm.selectMoHuQuotationTheme(quotationTheme);
	}
	/**
	 * 根据状态查询
	 * @param approvalStatus
	 * @return
	 */
	public JSONArray selectApprovalStatus(Integer approvalStatus){
		return qm.selectApprovalStatus(approvalStatus);
	}
	/**
	 * 修改状态【禁用】
	 * @param quotationId
	 * @return
	 */
	public int updateStatus(Integer quotationId) {
		return qm.updateStatus(quotationId);
		
	}
	/**
	 * 根据quoid查quo详情
	 * @param quotationId
	 * @return
	 */
	public JSONArray selectdetailspro(Integer quotationId){
		return qm.selectdetailspro(quotationId);
	}
	
	public List<Integer> selectcpId(Integer cpId,Integer quotationId) {
		return qm.selectcpId(cpId, quotationId);
		
	}
	/**
	 * 删除
	 */
	public void deleteDetails(Integer quotationDetailsId) {
		//先查询对象
		qddao.deleteById(quotationDetailsId);
	}
	
	/**
	 * 添加quo详情
	 * @param quotation
	 */
	public void addQuoDetails(Integer proId,Integer quoId,Integer quotationDetailsNumber) {
		Quotationdetail quo = new Quotationdetail();
		quo.setQuotationDetailsNumber(quotationDetailsNumber);
		Product proId2 =pdao.findById(proId).get();
		System.out.println("================="+proId);
		quo.setProduct2(proId2);
		Quotation quoId2 = qdao.findById(quoId).get();
		System.out.println("================="+quoId);
		quo.setQuotation(quoId2);
		qddao.save(quo);
	}
	/**
	 * 添加quo详情
	 * @param quotation
	 */
	public void updateQuonum(Integer quotationDetailsNumber,Integer quodelId) {
	
		qm.updateQuonum(quotationDetailsNumber,quodelId);
	}
}
